package com.bcpapp.project.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bcpapp.project.constant.RLConstant;
import com.bcpapp.project.model.entity.*;
import com.bcpapp.project.service.ReLiService;
import com.bcpapp.project.utils.FileUtils;
import com.bcpapp.project.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.*;


@Service
@Slf4j
public class ReLiServiceImpl implements ReLiService {


    @Value("${path.populationsituationdayPath}")
    private String populationsituationdayPath;

    @Value("${path.flowsumPath}")
    private String flowsumPath;

    @Value("${path.populationPath}")
    private String populationPath;

    @Value("${path.predictpopulationPath}")
    private String predictpopulationPath;

    @Value("${path.locationpointPath}")
    private String locationpointPath;

    @Value("${path.flowPath}")
    private String flowPath;

    @Value("${path.adcodemigrationPath}")
    private String adcodemigrationPath;

    @Value("${path.migrationuserprofilePath}")
    private String migrationuserprofilePath;

    @Value("${path.userprofilePath}")
    private String userprofilePath;

    @Value("${txrl.txrlkey}")
    private String txrlkey;

    @Value("${txrl.adcode}")
    private String adcode;

    @Value("${file.rlPopulationsituationday_F}")
    private String rlPopulationsituationday_F;

    @Value("${file.flowsum_F}")
    private String flowsum_F;

    @Value("${file.population_F}")
    private String population_F;

    @Value("${file.predictpopulation_F}")
    private String predictpopulation_F;


    @Value("${file.locationpoint_F}")
    private String locationpoint_F;


    @Value("${file.flow_F}")
    private String flow_F;


    @Value("${file.adcodemigration_F}")
    private String adcodemigration_F;

    @Value("${file.userprofileAlwaysPath_F}")
    private String userprofileAlwaysPath_F;


    @Value("${file.userprofileHomeAndAlwaysCity_F}")
    private String userprofileHomeAndAlwaysCity_F;


    @Value("${file.userprofileOtherTgi_F}")
    private String userprofileOtherTgi_F;

    @Value("${file.migrationuserprofile_F}")
    private String migrationuserprofile_F;


    /*
     * 区划迁入迁出驻留分析接口
     * "2023-11-06"
     * */
    @Override
    public void populationsituationday(String date) {


        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("date", date);
        paramMap.put("adcode", adcode);
        paramMap.put("key", txrlkey);

        JSON parse = JSONUtil.parse(paramMap);

        String result = HttpUtil.post(populationsituationdayPath, String.valueOf(parse));
        JSONObject resultJston = new JSONObject(result);
        if (!resultJston.get("status").toString().equals("0")) {

            log.info("调用异常");
            log.info(result);
        } else {
            JSONObject resultData = new JSONObject(resultJston.getStr("result"));


            Set<String> personTypeSet = resultData.keySet();
            List list = new ArrayList();
            //获取人口类型
            for (String personType : personTypeSet) {
                JSONObject userprofile = new JSONObject(resultData.get(personType));
                String populationCount = userprofile.get("population").toString();
                Set<String> userprofileKeySet = userprofile.keySet();
                userprofileKeySet.remove("population");
                //便利画像
                for (String userprofileKey : userprofileKeySet) {
                    JSONObject userprofileDetail = new JSONObject(userprofile.get(userprofileKey));
                    Set<String> detailsSet = userprofileDetail.keySet();

                    //便利画像属性
                    for (String detailsKey : detailsSet) {
                        JSONObject jsonObject = new JSONObject(userprofileDetail.get(detailsKey));
                        Map<String, Object> checkNumber = (Map) JSONUtil.parse(jsonObject);
                        //画像属性Key value
                        for (String key : checkNumber.keySet()) {
                            RlPopulationsituationday rlPopulationsituationday = new RlPopulationsituationday();
                            String value = checkNumber.get(key).toString();
                            //  System.out.println("key="+key+" vlaue="+value);
                            rlPopulationsituationday.setPersonType(RLConstant.personTypeMap.get(personType));
                            rlPopulationsituationday.setPersonCount(populationCount);
                            rlPopulationsituationday.setUserprofileType(RLConstant.PopulationsituationdayMap.get(detailsKey));
                            rlPopulationsituationday.setUserprofileContent(key);
                            rlPopulationsituationday.setUserprofilePcount(value);
                            rlPopulationsituationday.setCreateTime(date);

                            System.out.println(rlPopulationsituationday.toStringt());
                            list.add(rlPopulationsituationday);
                            //System.out.println(rlPopulationsituationday);

                        }

                    }

                }
            }


            String dateName = TimeUtils.getDateFormat();

            try {
                List<List> list1 = FileUtils.dataSize(list);
                for (int i = 0; i < list1.size(); i++) {
                    List<RlPopulationsituationday> tempList = list1.get(i);
                    String filename = "rlPopulationsituationday_" + dateName + "_" + i;

                    File file = new File(rlPopulationsituationday_F + "/" + filename + ".bcp");
                    //FileWriter fileWriter = new FileWriter(file);
                    for (int j = 0; j < tempList.size(); j++) {
                        RlPopulationsituationday rlPopulationsituationday = tempList.get(j);
                        FileUtil.appendString(rlPopulationsituationday.toStringt(), file, CharsetUtil.UTF_8);
                        FileUtil.appendString("\n", file, CharsetUtil.UTF_8);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /*
     * 区域实时累计人流接口
     *   jsonObject.put("id", "4136727468333852615");
        jsonObject.put("begin", 1699200000);
        jsonObject.put("end", 1699286400);
     * */
    @Override
    public void flowsum(String rlId, Integer beginT, Integer endT) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", rlId);
        jsonObject.put("begin", beginT);
        jsonObject.put("end", endT);
        jsonObject.put("people_type", "all");
        jsonObject.put("key", txrlkey);
        String result = HttpRequest.post(flowsumPath).header("Content-Type", "application/json")
                .body(jsonObject.toString()).timeout(30 * 1000).execute().body();

        JSONObject resultJston = new JSONObject(result);
        if (!resultJston.get("status").toString().equals("0")) {

            log.info("调用异常");
            log.info(String.valueOf(jsonObject));
            log.info(result);
        } else {
            JSONObject resultData = new JSONObject(resultJston.getStr("data"));
            Flowsum flowsum = new Flowsum();


            resultData.get("out");
            flowsum.setId(rlId);
            //flowsum.setBegin("1699200000");
            //flowsum.setEnd("1699286400");
            flowsum.setBegin(beginT.toString());
            flowsum.setEnd(beginT.toString());
            flowsum.setIn(Integer.valueOf(resultData.get("in").toString()));
            flowsum.setOut(Integer.valueOf(resultData.get("out").toString()));

            List list = new ArrayList();
            list.add(flowsum);

            System.out.println(flowsum.toStringt());

            String dateName = TimeUtils.getDateFormat();

            try {
                List<List> list1 = FileUtils.dataSize(list);
                for (int i = 0; i < list1.size(); i++) {
                    List<Flowsum> tempList = list1.get(i);
                    String filename = "flowsum_" + dateName + "_" + i;

                    File file = new File(flowsum_F + "/" + filename + ".bcp");
                    //FileWriter fileWriter = new FileWriter(file);
                    for (int j = 0; j < tempList.size(); j++) {
                        Flowsum flowsum1 = tempList.get(j);
                        FileUtil.appendString(flowsum1.toStringt(), file, CharsetUtil.UTF_8);
                        FileUtil.appendString("\n", file, CharsetUtil.UTF_8);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /*
     *
     *   https://apis.map.qq.com/bigdata/realtime/v1.1/population
     * 据区域实时人数接口
     * jsonObject.put("id", "4136727468333852615");
        jsonObject.put("begin", 1699200000);
        jsonObject.put("end", 1699286400);
     * */
    @Override
    public void population(String rlId, Integer beginT, Integer endT) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", rlId);
        jsonObject.put("begin", beginT);
        jsonObject.put("end", endT);
        jsonObject.put("interval", 5);
        jsonObject.put("people_type", "all");
        jsonObject.put("key", txrlkey);
        String result = HttpRequest.post(populationPath).header("Content-Type", "application/json")
                .body(jsonObject.toString()).timeout(30 * 1000).execute().body();
        JSONObject resultJston = new JSONObject(result);
        if (!resultJston.get("status").toString().equals("0")) {

            log.info("调用异常");
            log.info(String.valueOf(jsonObject));
            log.info(result);
        } else {
            JSONArray data = (JSONArray) resultJston.get("data");
            List list = new ArrayList();
            for (int i = 0; i < data.size(); i++) {
                JSONObject dataDetail = data.getJSONObject(i);
                Population population = new Population();
                population.setId(rlId);
                population.setTime(dataDetail.get("time").toString());
                population.setValue(dataDetail.get("value").toString());
                list.add(population);
                System.out.println(population.toStringt());

            }


            String dateName = TimeUtils.getDateFormat();

            try {
                List<List> list1 = FileUtils.dataSize(list);
                for (int i = 0; i < list1.size(); i++) {
                    List<Population> tempList = list1.get(i);
                    String filename = "population_" + dateName + "_" + i;

                    File file = new File(population_F + "/" + filename + ".bcp");
                    //FileWriter fileWriter = new FileWriter(file);
                    for (int j = 0; j < tempList.size(); j++) {
                        Population population = tempList.get(j);
                        FileUtil.appendString(population.toStringt(), file, CharsetUtil.UTF_8);
                        FileUtil.appendString("\n", file, CharsetUtil.UTF_8);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    /*
     *
     *
     * https://apis.map.qq.com/bigdata/realtime/v1.1/predictpopulation
     * 区域实时人数预测
     *   jsonObject.put("id", "4136727468333852615");
     * */
    @Override
    public void predictpopulation(String rlId) {

        log.info("----------------------进入区域实时人数预测predictpopulation");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", rlId);
        jsonObject.put("interval", 5);
        jsonObject.put("people_type", "all");
        jsonObject.put("key", txrlkey);
        String result = HttpRequest.post(predictpopulationPath).header("Content-Type", "application/json")
                .body(jsonObject.toString()).timeout(30 * 1000).execute().body();


        JSONObject resultJston = new JSONObject(result);
        if (!resultJston.get("status").toString().equals("0")) {

            log.info("调用异常");
            log.info(String.valueOf(jsonObject));
            log.info(result);
        } else {


            JSONArray data = (JSONArray) resultJston.get("data");

            List list = new ArrayList();

            for (int i = 0; i < data.size(); i++) {

                Map<String, String> mapdetails = (Map<String, String>) data.get(i);

                mapdetails.entrySet().forEach(entry -> {
                    Population population = new Population();
                    population.setId(rlId);
                    population.setTime(entry.getKey());
                    population.setValue(String.valueOf(entry.getValue()));
                    list.add(population);
                    System.out.println(population.toStringt());
                });

            }

            String dateName = TimeUtils.getDateFormat();

            try {
                List<List> list1 = FileUtils.dataSize(list);
                for (int i = 0; i < list1.size(); i++) {
                    List<Population> tempList = list1.get(i);
                    String filename = "predictpopulation_" + dateName + "_" + i;

                    File file = new File(predictpopulation_F + "/" + filename + ".bcp");
                    //FileWriter fileWriter = new FileWriter(file);
                    for (int j = 0; j < tempList.size(); j++) {
                        Population population = tempList.get(j);
                        FileUtil.appendString(population.toStringt(), file, CharsetUtil.UTF_8);
                        FileUtil.appendString("\n", file, CharsetUtil.UTF_8);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    /*
     * 区域实时人口热力
     *
     * https://apis.map.qq.com/bigdata/realtime/v1.1/locationpoint
     * */
    @Override
    public void locationpoint(String rlId, Integer beginT, Integer endT) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", rlId);
        jsonObject.put("begin", beginT);
        jsonObject.put("end", endT);
        jsonObject.put("interval", 5);
        jsonObject.put("people_type", "all");
        jsonObject.put("key", txrlkey);
        String result = HttpRequest.post(locationpointPath).header("Content-Type", "application/json")
                .body(jsonObject.toString()).timeout(30 * 1000).execute().body();
        JSONObject resultJston = new JSONObject(result);
        if (!resultJston.get("status").toString().equals("0")) {

            log.info("调用异常");
            log.info(String.valueOf(jsonObject));
            log.info(result);
        } else {
            JSONArray resultData = new JSONArray(resultJston.getStr("data"));
            Iterator<Object> iterator = resultData.stream().iterator();

            List list = new ArrayList();

            while (iterator.hasNext()) {
                JSONObject pointData = new JSONObject(iterator.next());
                String time = pointData.get("time").toString();
                JSONArray pointDetails = new JSONArray(pointData.get("points"));
                Iterator<Object> pointDetailsIterator = pointDetails.stream().iterator();
                while (pointDetailsIterator.hasNext()) {
                    JSONObject pointOne = new JSONObject(pointDetailsIterator.next());
                    Points points = JSONUtil.toBean(pointOne, Points.class);
                    Locationpoint locationpoint = new Locationpoint();
                    locationpoint.setId(rlId);
                    locationpoint.setPeopleType("活跃人口");
                    locationpoint.setTime(time);
                    locationpoint.setLat(points.getLat());
                    locationpoint.setLng(points.getLng());
                    locationpoint.setWeight(points.getWeight());
                    list.add(locationpoint);
                    System.out.println(locationpoint.toStringt());
                }
            }


            String dateName = TimeUtils.getDateFormat();

            try {
                List<List> list1 = FileUtils.dataSize(list);
                for (int i = 0; i < list1.size(); i++) {
                    List<Locationpoint> tempList = list1.get(i);
                    String filename = "locationpoint_" + dateName + "_" + i;

                    File file = new File(locationpoint_F + "/" + filename + ".bcp");
                    //FileWriter fileWriter = new FileWriter(file);
                    for (int j = 0; j < tempList.size(); j++) {
                        Locationpoint locationpoint = tempList.get(j);
                        FileUtil.appendString(locationpoint.toStringt(), file, CharsetUtil.UTF_8);
                        FileUtil.appendString("\n", file, CharsetUtil.UTF_8);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /*
     *
     * 区域实时人流接口
     *
     * https://apis.map.qq.com/bigdata/realtime/v1.1/flow
     * */
    @Override
    public void flow(String rlId, Integer beginT, Integer endT) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", rlId);
        jsonObject.put("begin", beginT);
        jsonObject.put("end", endT);
        jsonObject.put("interval", 5);
        jsonObject.put("people_type", "all");
        jsonObject.put("key", txrlkey);
        String result = HttpRequest.post(flowPath).header("Content-Type", "application/json")
                .body(jsonObject.toString()).timeout(60 * 1000).execute().body();
        JSONObject resultJston = new JSONObject(result);
        if (!resultJston.get("status").toString().equals("0")) {

            log.info("调用异常");
            log.info(String.valueOf(jsonObject));
            log.info(result);
        } else {
            JSONArray resultData = new JSONArray(resultJston.getStr("data"));
            Iterator<Object> iterator = resultData.stream().iterator();
            List list = new ArrayList();

            while (iterator.hasNext()) {
                JSONObject flowData = new JSONObject(iterator.next());
                Flow flow = new Flow();
                flow.setId("4136727468333852615");
                flow.setIn(flowData.get("in").toString());
                flow.setOut(flowData.get("out").toString());
                flow.setTime(flowData.get("time").toString());

                list.add(flow);
                //System.out.println(flow.toStringt());
            }

            // System.out.println(s);
           // System.out.println(result);

            String dateName = TimeUtils.getDateFormat();

            try {
                List<List> list1 = FileUtils.dataSize(list);
                for (int i = 0; i < list1.size(); i++) {
                    List<Flow> tempList = list1.get(i);
                    String filename = "flow_" + dateName + "_" + i;

                    File file = new File(flow_F + "/" + filename + ".bcp");
                    //FileWriter fileWriter = new FileWriter(file);
                    for (int j = 0; j < tempList.size(); j++) {
                        Flow flow = tempList.get(j);
                        FileUtil.appendString(flow.toStringt(), file, CharsetUtil.UTF_8);
                        FileUtil.appendString("\n", file, CharsetUtil.UTF_8);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /*

    区划迁徙总览接口的
    * https://apis.map.qq.com/bigdata/realtime/v1/adcodemigration
       paramMap.put("date", "2023-11-02");
    * */
    @Override
    public void adcodemigration(String date) {


        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("date", date);
        paramMap.put("adcode", adcode);
        paramMap.put("key", txrlkey);

        JSON parse = JSONUtil.parse(paramMap);

        String result = HttpUtil.post(adcodemigrationPath, String.valueOf(parse));
        JSONObject resultJston = new JSONObject(result);
        if (!resultJston.get("status").toString().equals("0")) {

            log.info("调用异常");
            log.info(String.valueOf(parse));
            log.info(result);
        } else {
            JSONObject resultData = new JSONObject(resultJston.getStr("result"));
            List list = new ArrayList();
            for (String key : RLConstant.adcodemigrationKey) {
                JSONObject adcodemigrationValue = new JSONObject(resultData.getStr(key));
                String population = adcodemigrationValue.get("population").toString();
                JSONObject traffic = new JSONObject(adcodemigrationValue.get("traffic"));
                String carTraffic = traffic.get("car").toString();
                String plainTraffic = traffic.get("plane").toString();
                String trainTraffic = traffic.get("train").toString();
                for (String vKey : RLConstant.adcodemigrationValueKey) {
                    JSONArray array = new JSONArray(adcodemigrationValue.get(vKey));
                    Iterator<Object> iterator = array.stream().iterator();
                    while (iterator.hasNext()) {
                        JSONObject pointData = new JSONObject(iterator.next());
                        Migration migration = JSONUtil.toBean(pointData, Migration.class);
                        Adcodemigration adcodemigration = new Adcodemigration();
                        adcodemigration.setDate(date);
                        adcodemigration.setAdcode(adcode);
                        adcodemigration.setDataType(RLConstant.adcodemigrationKeyMap.get(key));
                        adcodemigration.setPopulation(population);
                        adcodemigration.setSourceType(RLConstant.adcodemigrationValueKeyMap.get(vKey));
                        adcodemigration.setSourceAdcode(migration.getAdcode());
                        adcodemigration.setSourcePopulation(migration.getPopulation());
                        adcodemigration.setCarTraffic(carTraffic);
                        adcodemigration.setTrainTraffic(trainTraffic);
                        adcodemigration.setPlaneTraffic(plainTraffic);
                       // System.out.println(adcodemigration.toStringt());
                        list.add(adcodemigration);


                    }


                }
                //System.out.println("================================================");
            }

            String dateName = TimeUtils.getDateFormat();


            try {
                List<List> list1 = FileUtils.dataSize(list);
                for (int i = 0; i < list1.size(); i++) {
                    List<Adcodemigration> tempList = list1.get(i);
                    String filename = "adcodemigration_" + dateName + "_" + i;

                    File file = new File(adcodemigration_F + "/" + filename + ".bcp");
                    //FileWriter fileWriter = new FileWriter(file);
                    for (int j = 0; j < tempList.size(); j++) {
                        Adcodemigration adcodemigration = tempList.get(j);
                        FileUtil.appendString(adcodemigration.toStringt(), file, CharsetUtil.UTF_8);
                        FileUtil.appendString("\n", file, CharsetUtil.UTF_8);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /*
     * https://apis.map.qq.com/bigdata/realtime/v1/migrationuserprofile
     * 区划迁徙分析
     *       jsonObject.put("date", "2023-11-02");
        jsonObject.put("o_adcode", "410200");
        jsonObject.put("d_adcode", "610100");
     * */
    @Override
    public void migrationuserprofile(String date, String oAdcode, String dAdcode) {


        String dateName = TimeUtils.getDateFormat();
        List<Migrationuserprofile> migrationuserprofile1 = getMigrationuserprofile(dAdcode);
        try {
            log.info("写入文件");
            List<List> list1 = FileUtils.dataSize(migrationuserprofile1);
            for (int i = 0; i < list1.size(); i++) {
                List<Migrationuserprofile> tempList = list1.get(i);
                String filename = "migrationuserprofile_" + dateName + "_" + i;

                File file = new File(migrationuserprofile_F + "/" + filename + ".bcp");
                //FileWriter fileWriter = new FileWriter(file);
                for (int j = 0; j < tempList.size(); j++) {
                    Migrationuserprofile migrationuserprofile = tempList.get(j);
                    FileUtil.appendString(migrationuserprofile.toStringt(), file, CharsetUtil.UTF_8);
                    FileUtil.appendString("\n", file, CharsetUtil.UTF_8);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public List<Migrationuserprofile> getMigrationuserprofile(String dAdcode) {

        List migrationuserprofileList = new ArrayList();
        String lastDateStr = TimeUtils.getLastDateStr();
        RLConstant.adcode_2018.entrySet().forEach(entry -> {
            String key = entry.getKey();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("date", lastDateStr);
            jsonObject.put("o_adcode", key);
            jsonObject.put("d_adcode", dAdcode);
            jsonObject.put("count_type", 1);
            jsonObject.put("is_userprofile", 1);
            jsonObject.put("key", txrlkey);
            String result = HttpRequest.post(migrationuserprofilePath).header("Content-Type", "application/json")
                    .body(jsonObject.toString()).timeout(30 * 1000).execute().body();

            JSONObject resultJston = new JSONObject(result);
            if (!resultJston.get("status").toString().equals("0")) {
                return;
            }
            log.info(key);
            JSONObject resultData = new JSONObject(resultJston.getStr("result"));

            String population = resultData.get("population").toString();
            JSONObject userprofileJson = new JSONObject(resultData.get("userprofile"));
            JSONObject ageJson = new JSONObject(userprofileJson.get("age"));

            Map<String, String> ageMap = (Map) JSONUtil.parse(ageJson);
            JSONObject genderJson = new JSONObject(userprofileJson.get("gender"));
            Map<String, String> genderMap = (Map) JSONUtil.parse(genderJson);
            JSONObject trafficJson = new JSONObject(userprofileJson.get("traffic"));
            Map<String, String> trafficMap = (Map) JSONUtil.parse(trafficJson);

            Map<String, Map> allMap = new HashMap<>();
            allMap.put("age", ageMap);
            allMap.put("gender", genderMap);
            allMap.put("traffic", trafficMap);

            allMap.entrySet().forEach(entrys -> {
                Map<String, String> mm = entrys.getValue();
                mm.entrySet().forEach(entryset -> {
                    Migrationuserprofile migrationuserprofile = new Migrationuserprofile();
                    migrationuserprofile.setTime(lastDateStr);
                    migrationuserprofile.setO_adcode(key);
                    migrationuserprofile.setD_adcode(dAdcode);
                    migrationuserprofile.setPopulation(population);
                    migrationuserprofile.setUserprofile(entry.getKey());
                    migrationuserprofile.setUserprofileType(entryset.getKey());
                    migrationuserprofile.setValue(String.valueOf(entryset.getValue()));
                    migrationuserprofileList.add(migrationuserprofile);

                });

            });
        });


        return migrationuserprofileList;
    }

    /*
     * https://apis.map.qq.com/bigdata/realtime/v1.1/userprofile
     * 区域实时人口画像接口的
     * */
    @Override
    public void userprofile(String rlId, Integer beginT, Integer endT) {
        String dateName = TimeUtils.getDateFormat();

        Set<String> strings = RLConstant.userprofile_Type.keySet();
        String s = strings.toString();
        String types = "101010,101012,1121,1120,1612,1117,1413,1314,1116,1017,1610,1313,1312,1015,1410,1311,1014,1310,1112,1013,1012,1418,1416,1119,1110,1422,1421,1124,101015,1427";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", rlId);
        paramMap.put("begin", beginT);
        paramMap.put("end", endT);
        paramMap.put("interval", 60);
        paramMap.put("type", types);
        paramMap.put("people_type", "all");
        paramMap.put("key", txrlkey);
        JSON parse = JSONUtil.parse(paramMap);

        String result = HttpUtil.post(userprofilePath, String.valueOf(parse));

        JSONObject resultJston = new JSONObject(result);
        if (!resultJston.get("status").toString().equals("0")) {

            log.info("调用异常");
            log.info(String.valueOf(parse));
            log.info(result);
        } else {

            JSONArray resultArray = new JSONArray(resultJston.get("data"));
            Iterator<Object> iterator = resultArray.stream().iterator();

            List listAlwaysPath = new ArrayList();
            List listHomeAndAlwaysCity = new ArrayList();
            List UserprofileOtherTgi = new ArrayList();


            while (iterator.hasNext()) {
                String time = null;

                Map<String, Object> userprofileAll = new HashMap<>();

                JSONArray tempData = new JSONArray(iterator.next());
                Iterator<Object> tempDataIterator = tempData.stream().iterator();
                while (tempDataIterator.hasNext()) {
                    JSONArray tempData1 = new JSONArray(tempDataIterator.next());
                    for (int i = 0; i < tempData1.size(); i++) {
                        Map<String, Object> tempArr = (Map) JSONUtil.parse(tempData1.get(i));
                        for (int j = 0; j < RLConstant.userprofileList.size(); j++) {
                            if (tempArr.keySet().contains(RLConstant.userprofileList.get(j))) {
                                if (tempArr.keySet().contains("time")) {
                                    time = tempArr.get("time").toString();
                                } else if (tempArr.keySet().contains("常住地")) {

                                    JSONArray alwaysPathArray = new JSONArray(tempArr.get("常住地"));
                                    Iterator<Object> alwaysPathIter = alwaysPathArray.stream().iterator();
                                    while (alwaysPathIter.hasNext()) {
                                        AlwaysCity alwaysCity = JSONUtil.toBean((JSONObject) alwaysPathIter.next(), AlwaysCity.class);
                                        UserprofileAlwaysPath userprofileAlwaysPath = new UserprofileAlwaysPath();
                                        userprofileAlwaysPath.setId(rlId);
                                        userprofileAlwaysPath.setTime(time);
                                        userprofileAlwaysPath.setPathType("常住地");
                                        userprofileAlwaysPath.setCity(alwaysCity.getCity());
                                        userprofileAlwaysPath.setDistrict(alwaysCity.getDistrict());
                                        userprofileAlwaysPath.setPercent(alwaysCity.getPercent());
                                        userprofileAlwaysPath.setProperty(alwaysCity.getProperty());
                                        userprofileAlwaysPath.setProvince(alwaysCity.getProvince());
                                        listAlwaysPath.add(userprofileAlwaysPath);
                                        //System.out.println(userprofileAlwaysPath.toStringt());
                                    }

                                   // System.out.println("常住地");
                                } else {

                                    JSONArray homeCityArr = new JSONArray(tempArr.get("家乡地"));
                                    JSONArray alwaysCityArr = new JSONArray(tempArr.get("常驻城市"));

                                    JSONArray allCity = new JSONArray();
                                    allCity.addAll(homeCityArr);
                                    allCity.addAll(alwaysCityArr);

                                    Iterator<Object> alwaysPathIter = allCity.stream().iterator();
                                    while (alwaysPathIter.hasNext()) {
                                        HomeCity homeCity = JSONUtil.toBean((JSONObject) alwaysPathIter.next(), HomeCity.class);
                                        UserprofileHomeAndAlwaysCity userprofileHomeAndAlwaysCity = new UserprofileHomeAndAlwaysCity();

                                        userprofileHomeAndAlwaysCity.setId(rlId);
                                        userprofileHomeAndAlwaysCity.setTime(time);
                                        userprofileHomeAndAlwaysCity.setPathType("家乡地");
                                        userprofileHomeAndAlwaysCity.setCity(homeCity.getCity());
                                        userprofileHomeAndAlwaysCity.setPercent(homeCity.getPercent());
                                        userprofileHomeAndAlwaysCity.setProperty(homeCity.getProperty());
                                        userprofileHomeAndAlwaysCity.setProvince(homeCity.getProvince());
                                        listHomeAndAlwaysCity.add(userprofileHomeAndAlwaysCity);
                                       // System.out.println(userprofileHomeAndAlwaysCity.toStringt());
                                    }
                                    log.info("家乡地");
                                   // System.out.prilontln("家乡地");
                                }
                              //  System.out.println(tempArr);
                            }
                            ;
                            tempArr.remove(RLConstant.userprofileList.get(j));

                        }

                        if (tempArr.size() > 0) {
                            userprofileAll.putAll(tempArr);
                        }
                        //  System.out.println(tempArr.size());
                        // System.out.println(tempArr.toString());
                    }

                }


                try {
                    List<List> list1 = FileUtils.dataSize(listAlwaysPath);
                    for (int i = 0; i < list1.size(); i++) {
                        List<UserprofileAlwaysPath> tempList = list1.get(i);
                        String filename = "userprofileAlwaysPath_" + dateName + "_" + i;

                        File file = new File(userprofileAlwaysPath_F + "/" + filename + ".bcp");
                        //FileWriter fileWriter = new FileWriter(file);
                        for (int j = 0; j < tempList.size(); j++) {
                            UserprofileAlwaysPath userprofileAlwaysPath = tempList.get(j);
                            FileUtil.appendString(userprofileAlwaysPath.toStringt(), file, CharsetUtil.UTF_8);
                            FileUtil.appendString("\n", file, CharsetUtil.UTF_8);
                        }
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }


                try {
                    List<List> list1 = FileUtils.dataSize(listHomeAndAlwaysCity);
                    for (int i = 0; i < list1.size(); i++) {
                        List<UserprofileHomeAndAlwaysCity> tempList = list1.get(i);
                        String filename = "userprofileHomeAndAlwaysCity_" + dateName + "_" + i;

                        File file = new File(userprofileHomeAndAlwaysCity_F + "/" + filename + ".bcp");
                        //FileWriter fileWriter = new FileWriter(file);
                        for (int j = 0; j < tempList.size(); j++) {
                            UserprofileHomeAndAlwaysCity userprofileHomeAndAlwaysCity = tempList.get(j);
                            FileUtil.appendString(userprofileHomeAndAlwaysCity.toStringt(), file, CharsetUtil.UTF_8);
                            FileUtil.appendString("\n", file, CharsetUtil.UTF_8);
                        }
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }

               // System.out.println(userprofileAll);

                String finalTime = time;
                userprofileAll.entrySet().forEach(entry -> {
                    JSONArray ja = new JSONArray(entry.getValue());
                    Iterator<Object> iterator1 = ja.stream().iterator();
                    while (iterator1.hasNext()) {
                        Tgi tgi = JSONUtil.toBean((JSONObject) iterator1.next(), Tgi.class);
                        UserprofileOtherTgi userprofileOtherTgi = new UserprofileOtherTgi();
                        userprofileOtherTgi.setId(rlId);
                        userprofileOtherTgi.setTime(finalTime);
                        userprofileOtherTgi.setDataType(entry.getKey());
                        userprofileOtherTgi.setPercent(tgi.getPercent());
                        userprofileOtherTgi.setProperty(tgi.getProperty());
                        userprofileOtherTgi.setTgi_percent(tgi.getTgi_percent());
                        UserprofileOtherTgi.add(userprofileOtherTgi);
                       // System.out.println(userprofileOtherTgi.toStringt());
                    }
                    // System.out.println(            entry.getValue().toString());
                });

                try {
                    List<List> list1 = FileUtils.dataSize(UserprofileOtherTgi);
                    for (int i = 0; i < list1.size(); i++) {
                        List<UserprofileOtherTgi> tempList = list1.get(i);
                        String filename = "userprofileOtherTgi_" + dateName + "_" + i;

                        File file = new File(userprofileOtherTgi_F + "/" + filename + ".bcp");
                        //FileWriter fileWriter = new FileWriter(file);
                        for (int j = 0; j < tempList.size(); j++) {
                            UserprofileOtherTgi userprofileOtherTgi = tempList.get(j);
                            FileUtil.appendString(userprofileOtherTgi.toStringt(), file, CharsetUtil.UTF_8);
                            FileUtil.appendString("\n", file, CharsetUtil.UTF_8);
                        }
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
               // System.out.println("++++++++++++++");
            }


            //System.out.println(result);


        }
    }

}
